package com.sctvcloud.renhe.ui.utils;

import android.text.TextUtils;

import com.ruihang.generalibrary.db.DBUtils;
import com.sctvcloud.renhe.beans.FCollect;
import com.sctvcloud.renhe.beans.FProgram;
import com.sctvcloud.renhe.beans.NewsItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 收藏常用方法
 * Created by raoweiwei on 2017/8/9.
 */

public class CollectionUtils {
	private static CollectionUtils mUtils;
	private List<FCollect> mList;
	private boolean enableCache = false;

	public static CollectionUtils getInstance() {
		return getInstance(false);
	}

	public static CollectionUtils getInstance(boolean enableCache) {
		if (mUtils == null) {
			mUtils = new CollectionUtils();
			mUtils.enableCache = enableCache;
			if (enableCache) {
				mUtils.mList = mUtils.getCollectionListFromDB();
			}
		}
		return mUtils;
	}

	/**
	 * 返回按倒序排列的收藏列表（最新收藏的排在列表最顶部，最早收藏的排在最底部）
	 */
	public List<FCollect> getCollectionList() {
		List<FCollect> list = new ArrayList<>();
		list.addAll(enableCache ? mList : getCollectionListFromDB());
		Collections.reverse(list);
		return list;
	}

	public void clearCollectionList() {
		try {
			DBUtils.getInstance().deleteAll(FCollect.class);
		} catch (Exception e) {
		}
		if (enableCache) {
			mList.clear();
		}
	}

	/**
	 * 使用前必须判断对象非空
	 */
	public void addCollection(FCollect fCollect) {
		fCollect.setCollectionTime(System.currentTimeMillis());
		List<FCollect> list = enableCache ? mList : getCollectionListFromDB();
		list.add(fCollect);
		DBUtils.getInstance().replaceListAxync(list);
	}

	public void removeCollection(String id) {
		if (TextUtils.isEmpty(id)) {
			return;
		}
		List<FCollect> list = enableCache ? mList : getCollectionListFromDB();
		int index = findCollectionById(list, id);
		if (index != -1) {
			list.remove(index);
			if (list.size() == 0) {
				clearCollectionList();
			} else {
				//TODO 因DBUtils替换列表疑似存在BUG，所以删除单个收藏临时采用先清空数据库再重新添加的办法，添加了下面代码
				clearCollectionList();
				if (enableCache) {
					mList = list;
				}

				//原删除单个收藏的代码
				DBUtils.getInstance().replaceListAxync(list);
			}
		}
	}

	public boolean isCollected(String id) {
		if (TextUtils.isEmpty(id)) {
			return false;
		}
		return findCollectionById(enableCache ? mList : getCollectionListFromDB(), id) != -1 ;
	}

	private int findCollectionById(List<FCollect> list, String id) {
		int index = -1;
		for (int i = 0; i < list.size(); i++) {
			if (id.equals(list.get(i).getDataId())) {
				index = i;
				break;
			}
		}
		return index;
	}

	private List<FCollect> getCollectionListFromDB() {
		List<FCollect> list = null;
		try {
			list = DBUtils.getInstance().findAll(FCollect.class);
		} catch (Exception e) {
		}
		if (list == null) {
			list = new ArrayList<>();
		}
		return list;
	}

	public static FCollect convertData(NewsItem newsItem) {
		if (newsItem == null) {
			return null;
		}
		FCollect fCollect = new FCollect();
		fCollect.setId(newsItem.getNewsId());
		fCollect.setUrl(newsItem.getNewsUrl());
		fCollect.setTitle(newsItem.getNewsTitle());
		fCollect.setImg(newsItem.getNewsImage());
		fCollect.setTime(newsItem.getPubTime());
		fCollect.setDataType(newsItem.getNewsType());
		fCollect.setNewsMediaLength(newsItem.getNewsMediaLength());
		fCollect.setLable(newsItem.getLable());
		return fCollect;
	}

	public static FCollect convertData(FProgram fProgram) {
		if (fProgram == null) {
			return null;
		}
		FCollect fCollect = new FCollect();
		fCollect.setId(fProgram.getProgrammeId());
		fCollect.setUrl(fProgram.getProgrammeUrl());
		fCollect.setTitle(fProgram.getProgrammeTitle());
		fCollect.setImg(fProgram.getProgrammeImage());
		fCollect.setTime(fProgram.getPubTime());
		fCollect.setDataType(fProgram.getDataType());
		return fCollect;
	}

	public static NewsItem recoveryData(FCollect fCollect) {
		if (fCollect == null) {
			return null;
		}
		NewsItem newsItem = new NewsItem();
		newsItem.setNewsId(fCollect.getId());
		newsItem.setNewsUrl(fCollect.getUrl());
		newsItem.setNewsTitle(fCollect.getTitle());
		newsItem.setNewsImage(fCollect.getDataImg());
		newsItem.setPubTime(fCollect.getTime());
		newsItem.setNewsType(fCollect.getDataType());
		if(!TextUtils.isEmpty(fCollect.getSub_news_url())){
			newsItem.setSub_news_url(fCollect.getSub_news_url());
		}
		return newsItem;
	}
}
